package com.mobilepay.moneypig.base;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mobilepay.moneypig.utils.FragmentManager;
import com.mobilepay.moneypig.utils.LogDebug;
import com.mobilepay.moneypig.utils.inject.InjectUtil;

/**
 * Created by xlm on 2017/7/12 0012
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "BaseFragment";
    public View mLayoutView;
    private int mLayoutId = -1;
    protected BaseFragment mThis;
    private FragmentManager mFragmentManager;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mThis = this;
        Bundle bundle = getArguments();
        if (bundle != null) {
            onGetArguments(bundle);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        beforeSetContentView();
        onSetContentView();
        if (mLayoutId != -1) {
            mLayoutView = inflater.inflate(mLayoutId, container, false);
        }
        afterSetContentView();
        return mLayoutView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        onFindView(mLayoutView);
        initViews();
        afterOnCreate(savedInstanceState);
        onSetListener();
    }

    public void setContentView(int id) {
        mLayoutId = id;
    }

    protected void onBackPressed() {
    }

    /**
     * 通过注解自动绑定资源ID
     */
    private void injectView() {
        InjectUtil.injectFragmentFields(this, mLayoutView);
    }

    public BaseFragment getThis() {
        return mThis;
    }

    public View getLayoutView() {
        return mLayoutView;
    }

    /**
     * 判断是否是初始化Fragment
     */
    private boolean hasStarted = false;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogDebug.v(TAG, "setUserVisibleHint " + isVisibleToUser);
        if (isVisibleToUser) {
            hasStarted = true;
            onVisible();
            LogDebug.v(TAG, "开始界面");
        } else {
            if (hasStarted) {
                hasStarted = false;
                onInVisible();
                LogDebug.v(TAG, "结束界面");
            }
        }
    }

    /**
     * 配合viewpager使用时，如果要完全切换过来时才需要执行的方法在onVisible里面写
     */
    public void onVisible() {
    }

    public void onInVisible() {
    }

    public FragmentManager initFragmentManager(int framlayout) {
        mFragmentManager = new FragmentManager(getThis(), framlayout);
        return mFragmentManager;
    }

    public void showToastShort(String msg) {
        ((BaseActivity) getActivity()).showToastShort(msg);
    }

    public void showToastLong(String msg) {
        ((BaseActivity) getActivity()).showToastLong(msg);
    }

    public ProgressDialog getProgressDialog(String msg) {
        return ((BaseActivity) getActivity()).getProgressDialog(msg);
    }

    public void showProgress(String msg) {
        ((BaseActivity) getActivity()).showProgress(msg);
    }

    public void showDownProgress(String msg) {
        ((BaseActivity) getActivity()).showDownProgress(msg);
    }

    public void dismissProgress() {
        ((BaseActivity) getActivity()).dismissProgress();
    }

    public void dismissDownProgress() {
        ((BaseActivity) getActivity()).dismissDownProgress();
    }

    public void startActivity(Class<?> cls) {
        ((BaseActivity) getActivity()).startActivity(cls, false, false);
    }

    public void startActivity(Class<?> cls, boolean finish, boolean delay) {
        ((BaseActivity) getActivity()).startActivity(cls, false, false);
    }

    protected void onGetArguments(Bundle bundle) {

    }

    protected void beforeSetContentView() {

    }

    protected void afterSetContentView() {

    }

    protected void onFindView(View view) {
    }

    @Override
    public void onClick(View v) {

    }

    protected abstract void onSetContentView();//初始化布局文件

    protected abstract void initViews();//绑定view

    protected abstract void afterOnCreate(Bundle savedInstanceState);//事件操作

    protected abstract void onSetListener();//设置监听

    public boolean checkStoragePermission(int request_code) {
        int permissionr = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
        int permissionw = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permissionr == PackageManager.PERMISSION_GRANTED && permissionw == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            String[] PERMISSIONS_STORAGE = {
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            };
            ActivityCompat.requestPermissions(getActivity(), PERMISSIONS_STORAGE,
                    request_code);
            return false;
        }
    }

    public boolean checkCameraPermission(int request_code) {
        int permission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA);
        if (permission == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            String[] PERMISSIONS_STORAGE = {
                    Manifest.permission.CAMERA
            };
            ActivityCompat.requestPermissions(getActivity(), PERMISSIONS_STORAGE,
                    request_code);
            return false;
        }
    }

    public boolean checkRecordAudioPermission(int request_code) {
        int permission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.RECORD_AUDIO);
        if (permission == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            String[] PERMISSIONS_STORAGE = {
                    Manifest.permission.RECORD_AUDIO
            };
            ActivityCompat.requestPermissions(getActivity(), PERMISSIONS_STORAGE,
                    request_code);
            return false;
        }
    }

    public boolean checkLocationPermission(int request_code) {
        int permissionf = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION);
        int permissionc = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION);
        if (permissionf == PackageManager.PERMISSION_GRANTED && permissionc == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            String[] PERMISSIONS_STORAGE = {
                    Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION
            };
            ActivityCompat.requestPermissions(getActivity(), PERMISSIONS_STORAGE,
                    request_code);
            return false;
        }
    }

    public boolean checkReadPhoneStatePermission(int request_code) {
        int permissionf = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE);
        if (permissionf == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            String[] PERMISSIONS_STORAGE = {
                    Manifest.permission.READ_PHONE_STATE};
            ActivityCompat.requestPermissions(getActivity(), PERMISSIONS_STORAGE,
                    request_code);
            return false;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (permissions[0]) {
            case Manifest.permission.READ_EXTERNAL_STORAGE:
            case Manifest.permission.WRITE_EXTERNAL_STORAGE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                    onStoragePermission(requestCode, true);
                } else {
                    onStoragePermission(requestCode, false);
                }
                break;
            case Manifest.permission.CAMERA:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    onCameraPermission(requestCode, true);
                } else {
                    onCameraPermission(requestCode, false);
                }
                break;
            case Manifest.permission.RECORD_AUDIO:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    onRecordAudioPermission(requestCode, true);
                } else {
                    onRecordAudioPermission(requestCode, false);
                }
                break;
            case Manifest.permission.ACCESS_FINE_LOCATION:
            case Manifest.permission.ACCESS_COARSE_LOCATION:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                    onLocationPermission(requestCode, true);
                } else {
                    onLocationPermission(requestCode, false);
                }
                break;
            case Manifest.permission.READ_PHONE_STATE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    onReadPhoneStatePermission(requestCode, true);
                } else {
                    onReadPhoneStatePermission(requestCode, false);
                }
                break;
        }
    }

    public void onStoragePermission(int result_code, boolean result) {

    }

    public void onCameraPermission(int result_code, boolean result) {

    }

    public void onRecordAudioPermission(int result_code, boolean result) {

    }

    public void onLocationPermission(int result_code, boolean result) {

    }

    public void onReadPhoneStatePermission(int result_code, boolean result) {

    }
}
